<?php

namespace App\Http\Controllers\admin\system;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use lxs\aliyun\OssStorage;
use lxs\api\ApiResponse;

class UploadController extends Controller
{


    /**
     * 本地上传
     */
    public function uploadsSubmit(Request $request): JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'file' => [
                "required",
                "file",
                "mimes:jpeg,jpg,png,gif",
                "max:2048",
            ]
        ], [
            'file.required' => '请上传图片',
            'file.file' => '请上传图片',
            'file.mimes' => '上传图片只能是 JPG 或 PNG 或 JPEG 格式!',
            'file.max' => '上传图片大小不能超过2MB',
        ]);
        if ($validator->fails()) {
            return ApiResponse::apiResponseData(400, $validator->errors()->first());
        }

        $file = $request->file('file');
        $path = request()->input('name');
        if (isset($path)) {
            $path = 'admin/' . $path;
        }

        $url = (new OssStorage())->Uploads($file, $path);

        return ApiResponse::apiResponseData(200, '获取图片数据成功', [
            'url' => $url
        ]);
    }


}
